//
//  ThreeViewController.m
//  GCD
//
//  Created by fangyuan ding on 2019/12/20.
//  Copyright © 2019年 fangyuan ding. All rights reserved.
//

#import "ThreeViewController.h"

@interface ThreeViewController ()

@property (nonatomic, strong)NSMutableArray *photoList;

@end

@implementation ThreeViewController
- (NSMutableArray *)photoList {
    if(!_photoList){
        _photoList = [NSMutableArray array];
    }
    return _photoList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //异步 子线程
//    [self demo1];
//    for(int i = 0; i < 1000; i++){
//        [self downLoadImage:i];
//    }
    //延迟执行
    /*
     参数
     1.延迟多长时间 精度到秒
     2.队列
     3.任务
     */
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"task");
    });
//    //一次性执行
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//         NSLog(@"%@", [NSThread currentThread]);
//    });
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //一次性执行
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"%@", [NSThread currentThread]);
    });
}
- (void)downLoadImage:(int)index{
    dispatch_queue_t queue = dispatch_queue_create("hm", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSString *fileName = @"2.png";
        NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
        UIImage *img = [UIImage imageWithContentsOfFile:path];
        dispatch_barrier_async(queue, ^{
            [self.photoList addObject:img];
            NSLog(@"保存图片%d %@",index,[NSThread currentThread]);
        });
        NSLog(@"下载完成%d %@",index,[NSThread currentThread]);
    });
    
}
- (void)demo1{
    //异步 子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"输入密码%@",[NSThread currentThread]);
        });
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"扣费%@",[NSThread currentThread]);
            [NSThread sleepForTimeInterval:1];
        });
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"下载应用%@",[NSThread currentThread]);
        });
        
    });
}


@end
